package com.slz.crm.server.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.slz.crm.common.annotation.RequirePermission;
import com.slz.crm.common.enumeration.PermissionOperates;
import com.slz.crm.common.exiception.BaseException;
import com.slz.crm.common.result.Result;
import com.slz.crm.pojo.dto.ApprovalAttachmentDTO;
import com.slz.crm.pojo.dto.SalesStageApprovalANDAttachmentDTO;
import com.slz.crm.pojo.dto.SalesStageApprovalDTO;
import com.slz.crm.pojo.dto.SalesStageApprovalPageDTO;
import com.slz.crm.pojo.entity.SalesStageApprovalEntity;
import com.slz.crm.pojo.vo.ApprovalAttachmentVO;
import com.slz.crm.pojo.vo.SalesStageApprovalVO;
import com.slz.crm.pojo.vo.UpdateSalesStageApprovalDTO;
import com.slz.crm.server.constant.MessageConstant;
import com.slz.crm.server.service.ApprovalAttachmentService;
import com.slz.crm.server.service.SalesStageApprovalService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 销售机会阶段审批
 */
@RestController
@RequestMapping("/sales/stage")
@Slf4j
public class SalesStageApprovalController {


    @Autowired
    private SalesStageApprovalService salesStageApprovalService;

    @Autowired
    private ApprovalAttachmentService approvalAttachmentService;

    /**
     * 推进销售机会阶段
     *
     * @param dto 销售机会阶段审批DTO
     * @return 销售机会阶段审批VO分页列表
     */
    @PostMapping("/approval")
    @Transactional
    @RequirePermission(
            permClass = PermissionOperates.SalesOperate.class,
            permName = "PROGRESS_SALE_OPPORTUNITY_STAGE"
    )
    public Result<SalesStageApprovalVO> updateStage(@ModelAttribute SalesStageApprovalANDAttachmentDTO dto) {

        if (dto.getApprovalAttachment() == null || dto.getApprovalAttachment().isEmpty()) {
            throw new BaseException(MessageConstant.APPROVAL_ATTACHMENT_IS_NULL);
        }

        SalesStageApprovalVO vo = salesStageApprovalService.approveStage(dto.getSalesStageApproval());
        List<ApprovalAttachmentDTO> approvalAttachmentDTO = dto.getApprovalAttachment();
        if (approvalAttachmentDTO != null && !approvalAttachmentDTO.isEmpty()) {
            for (ApprovalAttachmentDTO attachmentDTO : approvalAttachmentDTO) {
                attachmentDTO.setApprovalId(vo.getId());
            }
            approvalAttachmentService.saveBatch(approvalAttachmentDTO);
        }
        return Result.success(vo);
    }

    /**
     * 审批推进请求
     */
    @PutMapping
    @RequirePermission(
            permClass = PermissionOperates.SalesOperate.class,
            permName = "APPROVE_STAGE_ADVANCE"
    )
    public Result<Boolean> updateStage(@ModelAttribute UpdateSalesStageApprovalDTO dto) {

        if (dto.getApprovalStatus() == null) {
            throw new BaseException("审批状态不能为空");
        }

        SalesStageApprovalDTO ans = new SalesStageApprovalDTO();
        BeanUtils.copyProperties(dto, ans);

        ans.setApprovalStatus(List.of(dto.getApprovalStatus()));
        return Result.success(salesStageApprovalService.updateById(ans));
    }

    /**
     * 删除推进请求
     */
    @DeleteMapping
    @Transactional
    @RequirePermission(
            permClass = PermissionOperates.SalesOperate.class,
            permName = "DELETE_STAGE_ADVANCE"
    )
    public Result<Boolean> deleteStage(@RequestParam List<Long> ids) {
        approvalAttachmentService.removeByApprovalIds(ids);
        salesStageApprovalService.removeByIds(ids);
        return Result.success(true);
    }

    /**
     * 自定义查询
     *
     * @param dto 销售机会阶段审批DTO
     * @return 销售机会阶段审批VO分页列表
     */
    @GetMapping
    @RequirePermission(
            permClass = PermissionOperates.SalesOperate.class,
            permName = "CUSTOM_QUERY_SALE_OPPORTUNITY_STAGE"
    )
    public Result<Page<SalesStageApprovalVO>> getStage(SalesStageApprovalPageDTO dto) {

        if (dto.getPageNum() == null || dto.getPageSize() == null || dto.getPageNum() <= 0 || dto.getPageSize() <= 0) {
            dto.setPageSize(10);
            dto.setPageNum(1);
        }
        Page<SalesStageApprovalEntity> page = new Page<>(dto.getPageNum(), dto.getPageSize());
        return Result.success(salesStageApprovalService.getPage(page, dto.getDto()));
    }

    /**
     * 通过销售机会阶段审批ID获取附件
     */
    @GetMapping("/attachment")
    @RequirePermission(
            permClass = PermissionOperates.SalesOperate.class,
            permName = "GET_APPROVAL_ATTACHMENT_BY_APPROVAL_ID"
    )
    public Result<List<ApprovalAttachmentVO>> getApprovalAttachment(@RequestParam List<Long> approvalIds) {
        if (approvalIds == null || approvalIds.isEmpty()) {
            return Result.success(null);
        }
        return Result.success(approvalAttachmentService.getByApprovalIds(approvalIds));
    }


}
